home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12727 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: grantp@usa.pipeline.com(Pete Grant)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Allocating memory in windows programs
  5. Date: 21 Mar 1996 11:49:59 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4irfp7$g0u@news1.h1.usa.pipeline.com>
  8. References: <4ir0j0$2ge@dsk92.itg.ti.com>
  9. NNTP-Posting-Host: 38.8.120.9
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete Grant)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Mar 21, 1996 07:30:40 in article <Allocating memory in windows
  16. programs>, 'dwnw%mimi@magic.itg.ti.com (Doug Nintzel)' wrote: 
  17.  
  18.  
  19. >Hello, I am new at windows programming. I am using MS-SDK/c++. I am using
  20. the  
  21. >below code to allocate memory.... 
  22. >/****************************************************************************/ 
  23. >memtst() 
  24. >{ 
  25. >char FAR*str; 
  26. >i=0; 
  27. >/*  
  28. >if((str=(char*)malloc(sizev))==NULL){MessageBox(hWnd,"GlobLock()failed","globl 
  29. >ock",MB_OK);return(0);} 
  30. >for(i=0;i<555;++i)str[i]='x';str[25]='\0'; 
  31. >*/ 
  32. >if((hmem=GlobalAlloc(GHND,100))==NULL){MessageBox(hWnd,"GlobAlloc(), 
  33. >    failed",MB_OK);return(0);} 
  34. >str=(char FAR*)GlobalLock(hmem); 
  35. >if(str==NULL){MessageBox(hWnd,"GlobLock()failed","globlock",MB_OK);return(0);} 
  36. >MessageBox(hWnd,str,"ALLOC/LOCK OK",MB_OK); 
  37. >strcpy(str,"mem test ok"); 
  38. >MessageBox(hWnd,str,"str content",MB_OK); 
  39. >} 
  40. >/****************************************************************************/ 
  41. >...it crashes at strcpy(str,"mem test ok");... 
  42. >I have also tried to used the commented out malloc() test instead of      
  43. >GlobalAlloc(). 
  44. >I have tried several other changes/combinations w/no luck.  
  45. >I have also checked c++ FAQ's and checked several book stores. 
  46. >I am running out of ideas!!!...Please help..... 
  47. Most likely cause:  You're building with a small or equivalent 
  48. memory model.  Standard solution is to switch to a large model, 
  49. however, some compilers have an alternate form of str.. functions 
  50. of the form fstr.. that accept far pointers.  IMHO you should 
  51. use the large memory model to avoid problems elsewhere. 
  52.  
  53. FYI:  The small MM library calls accept only near pointers.   
  54. GlobalLock returns a far pointer.  When you pass a far pointer 
  55. (doesn't you compiler warn about this??) where a near one is 
  56. expected, you end up with an address that is probably invalid -- 
  57. hence the crash. 
  58.  
  59. I see nothing else wrong in your sample code so it sure looks 
  60. like a memory model mismatch problem. 
  61. -- 
  62. Pete Grant 
  63. Kalevi, Inc. 
  64. Software Engineering & development
  65.